﻿using Sasc.PV.Common;
using Sasc.PV.DAL;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sasc.PV.BLL
{
    public abstract class FanGongableCheJianManagement<TCheJianFanGongGongXu, TCheJianGongXuTarget> : CheJianManagementBase<TCheJianGongXuTarget>
        where TCheJianGongXuTarget : class, ICheJianGongXuTarget, new()
        where TCheJianFanGongGongXu : class,ICheJianFanGongGongXu, new()
    {
        private static readonly Hashtable cachedFanGongVersion = new Hashtable();
        /// <summary>
        /// 防止构造方法死循环
        /// </summary>
        private TCheJianFanGongGongXu _fanGongGongXu;
        public TCheJianFanGongGongXu FanGongGongXu
        {
            get
            {
                if (_fanGongGongXu == null)
                    _fanGongGongXu = new TCheJianFanGongGongXu();
                return _fanGongGongXu;
            }
        }

        #region Ctor
        public FanGongableCheJianManagement()
        {
        }
        #endregion

        #region GetCurrentFanGongVersion
        protected virtual int GetCurrentFanGongVersion<TCheJian>(CheJianGongXuBase<TCheJian, TCheJianGongXuTarget> gongxu, TCheJianGongXuTarget target)
            where TCheJian : FanGongableCheJianManagement<TCheJianFanGongGongXu, TCheJianGongXuTarget>, new()
        {
            if (cachedFanGongVersion.Contains(target.GetHashCode()))
                return (int)cachedFanGongVersion[target.GetHashCode()];

            var result = FanGongGongXu.GetCurrentFanGongVersion(target);
            cachedFanGongVersion[target.GetHashCode()] = result;
            return result;
        }
        #endregion

        #region GenerateOtherExistWhereSQL
        protected override string GenerateOtherCommonExistWhereSQL<TCheJian>(CheJianGongXuBase<TCheJian, TCheJianGongXuTarget> gongxu, TCheJianGongXuTarget target)
        {
            var fanGongVerion = FanGongGongXu.GetCurrentFanGongVersion(target);
            return string.Format(" {0}={1} ", 
                DbMappingHelper.GetDbFieldName<IPvFanGongableGongXuRecModel, int>(c => c.FanGongVersion),
                fanGongVerion);
        }
        #endregion
    }
}
